package com.sina.shop.domain.admin;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.sina.shop.core.domain.Domain;
import com.sina.shop.domain.Status;
import com.sina.shop.domain.address.Address;
import com.sina.shop.domain.business.Business;
import com.sina.shop.domain.card.Card;
import com.sina.shop.domain.collect.Collect;
import com.sina.shop.domain.foot.Foot;
import com.sina.shop.domain.goods.GoodsComment;
import com.sina.shop.domain.log.Log;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 用户类
 * 段浩杰   2017-12-08-17:17
 */
@Entity
@Table(name = "tb_admin")
@DynamicInsert
@DynamicUpdate
public class Admin extends Domain {

    @Id
    @Column(name = "id", updatable = false)
    @GenericGenerator(name = "sequence_id", strategy = "com.sina.shop.sequence.IdGenerator")
    @GeneratedValue(generator = "sequence_id")
    @Min(1)
    private Long id;//主键

    @Column(name = "name", length = 15)
    private String name;//用户名

    @Column(name = "real_name", length = 15, updatable = false)
    private String realName;//真实姓名

    @Column(name = "account", updatable = false, length = 15)
    private String account;//账户

    @Column(name = "password", length = 34)
    private String password;//密码

    @Column(name = "credit", length = 5, insertable = false)
    private Integer credit;//信誉度

    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 10)
    private Status status;//状态

    @Column(name = "photo", length = 50)
    private String photo;//头像

    @Enumerated(EnumType.STRING)
    @Column(name = "sex", length = 10)
    private UserSex sex;//性别

    @Column(name = "phone", length = 12)
    private String phone;//电话

    @Column(name = "email", length = 20)
    private String email;//邮箱

    @Column(name = "create_time", updatable = false)
    @ColumnDefault("CURRENT_TIMESTAMP")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime createTime;//创建时间

    @Column(name = "previous_login_time")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime previousLoginTime;//上次登录时间

    @Column(name = "id_card", length = 20)
    private String idCard;//身份证

    @Column(name = "address", length = 50)
    private String address;//住址

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "tb_admin_role", joinColumns = @JoinColumn(name = "aid"), inverseJoinColumns = @JoinColumn(name = "rid"))
    private Set<Role> roles;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "admin", fetch = FetchType.LAZY)
    private Set<Business> businesses;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "admin", fetch = FetchType.LAZY)
    private Set<GoodsComment> goodsComments;

    @JsonIgnore
    @OneToMany(mappedBy = "admin", fetch = FetchType.LAZY)
    //@Transient
    private Set<Card> cards;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "admin", fetch = FetchType.LAZY)
    private Set<Address> addresses;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "admin", fetch = FetchType.LAZY)
    private Set<Foot> feet;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "admin", fetch = FetchType.LAZY)
    private Set<Log> logs;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "admin", fetch = FetchType.LAZY)
    private Set<Collect> collects;


    public Admin() {
    }

    public Admin(Long id, String name, String account) {
        this.id = id;
        this.name = name;
        this.account = account;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getCredit() {
        return credit;
    }

    public void setCredit(Integer credit) {
        this.credit = credit;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public UserSex getSex() {
        return sex;
    }

    public void setSex(UserSex sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getPreviousLoginTime() {
        return previousLoginTime;
    }

    public void setPreviousLoginTime(LocalDateTime previousLoginTime) {
        this.previousLoginTime = previousLoginTime;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public Set<Business> getBusinesses() {
        return businesses;
    }

    public void setBusinesses(Set<Business> businesses) {
        this.businesses = businesses;
    }

    public Set<GoodsComment> getGoodsComments() {
        return goodsComments;
    }

    public void setGoodsComments(Set<GoodsComment> goodsComments) {
        this.goodsComments = goodsComments;
    }

    public Set<Card> getCard() {
        return cards;
    }

    public void setCard(Set<Card> cards) {
        this.cards = cards;
    }

    public Set<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }

    public Set<Foot> getFeet() {
        return feet;
    }

    public void setFeet(Set<Foot> feet) {
        this.feet = feet;
    }

    public Set<Log> getLogs() {
        return logs;
    }

    public void setLogs(Set<Log> logs) {
        this.logs = logs;
    }

    public Set<Collect> getCollects() {
        return collects;
    }

    public void setCollects(Set<Collect> collects) {
        this.collects = collects;
    }
}